import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getx_controller_lifecycle/3%E3%80%81GetxController%E4%B8%8D%E9%9A%8F%E8%B7%AF%E7%94%B1%E9%80%80%E5%87%BA%E8%80%8C%E9%94%80%E6%AF%81/permanent_controller1.dart';
import 'package:getx_controller_lifecycle/3%E3%80%81GetxController%E4%B8%8D%E9%9A%8F%E8%B7%AF%E7%94%B1%E9%80%80%E5%87%BA%E8%80%8C%E9%94%80%E6%AF%81/permanent_controller2.dart';
import 'package:getx_controller_lifecycle/3%E3%80%81GetxController%E4%B8%8D%E9%9A%8F%E8%B7%AF%E7%94%B1%E9%80%80%E5%87%BA%E8%80%8C%E9%94%80%E6%AF%81/permanent_controller3.dart';
import '../home_page.dart';

/// 1、在Get.put的方法中设置permanent=true能避免controller随页面退出而销毁
/// 此案例中，PermanentController2正确设置了permanent=true，在退出页面时，不会从底层的Map删除
///
/// 2、Get.put在第一次有效设置之前，会被覆盖
/// 此案例中，PermanentController3定义为了全局，并且设置了permanent=true，
/// 但是由于在build有进行了一次Get.put且没有设置permanent=true，所以在页面退出的时候，还是会被销毁
/// 可以尝试将注释1的代码注释调，PermanentController3在退出页面时就不会被销毁了
///

final PermanentController3 controller3 = Get.put(PermanentController3(), permanent: true);

class PermanetPage extends StatelessWidget {
  PermanetPage({super.key});

  final PermanentController1 controller1 = Get.put(PermanentController1());
  final PermanentController2 controller2 = Get.put(PermanentController2(), permanent: true);

  _addCount1() {
    controller1.counter += 1;
  }

  _addCount2() {
    controller2.counter += 1;
  }

  _addCount3() {
    controller3.counter += 1;
  }

  @override
  Widget build(BuildContext context) {
    final PermanentController2 controller2 = Get.put(PermanentController2());
    //注释1
    final PermanentController3 controller3 = Get.put(PermanentController3());
    PageAction action = Get.arguments;

    return Scaffold(
        appBar: AppBar(
          title: Text(
            action.name,
            style: const TextStyle(
                fontSize: 14
            ),
          ),
        ),
        backgroundColor: Colors.white,
        body: SingleChildScrollView(
          child: Column(
            children: [
              const Padding(
                padding: EdgeInsets.all(15),
                child: Text(
                  """
1、在Get.put的方法中设置permanent=true能避免controller随页面退出而销毁
2、Get.put在第一次有效设置之前，会被覆盖
                  """,
                  maxLines: 100,
                  style: TextStyle(
                    fontSize: 15,
                  ),
                  strutStyle: StrutStyle(
                      leading: 0
                  ),
                ),
              ),
              Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Obx(() {
                    return Text(
                        "count1:${controller1.counter}"
                    );
                  }),
                  Obx(() {
                    return Text(
                        "count2:${controller2.counter}"
                    );
                  }),
                  Obx(() {
                    return Text(
                        "count3:${controller3.counter}"
                    );
                  }),
                  const SizedBox(height: 15,),
                  ElevatedButton(
                      onPressed: _addCount1,
                      child: const Text("count1 + 1")
                  ),
                  ElevatedButton(
                      onPressed: _addCount2,
                      child: const Text("count2 + 1")
                  ),
                  ElevatedButton(
                      onPressed: _addCount3,
                      child: const Text("count3 + 1")
                  )
                ],
              ),
            ],
          ),
        )
    );
  }
}